
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>“什么是BFC” - Bittersweet.的博客</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="bittersweet,"> 
    <meta name="description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,你以为我要先讲什么是bfc吗？1.overflow是什么？
根据MDN的定义：**overflow** 是 CSS 的简写属性，其设置了元素溢出时所需的行为——即当元素的内容太大而无法适应它的块级格,"> 
    <meta name="author" content="Bittersweet.(姚亮)"> 
    <link rel="alternative" href="atom.xml" title="Bittersweet.的博客" type="application/atom+xml"> 
    <link rel="icon" href="/Bittersweet/img/favicon.ico"> 
    
    <meta name="twitter:image:src" content="https://bangdream.moe/img/ident.png"/>
    <meta name="twitter:creator" content="@yourtwitteridhere"/>
    <meta name="twitter:card" content="summary_large_image"/>
    <meta name="twitter:title" content="“什么是BFC” - Bittersweet.的博客"/>
    <meta name="twitter:description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,你以为我要先讲什么是bfc吗？1.overflow是什么？
根据MDN的定义：**overflow** 是 CSS 的简写属性，其设置了元素溢出时所需的行为——即当元素的内容太大而无法适应它的块级格,"/>
    <meta property="og:image" content="https://bangdream.moe/img/ident.png"/>
    <meta property="og:image:alt" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,你以为我要先讲什么是bfc吗？1.overflow是什么？
根据MDN的定义：**overflow** 是 CSS 的简写属性，其设置了元素溢出时所需的行为——即当元素的内容太大而无法适应它的块级格,"/>
    <meta property="og:image:width" content="1200"/>
    <meta property="og:image:height" content="600"/>
    <meta property="og:site_name" content="Bittersweet.的博客"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="“什么是BFC” - Bittersweet.的博客"/>
    <meta property="og:description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,你以为我要先讲什么是bfc吗？1.overflow是什么？
根据MDN的定义：**overflow** 是 CSS 的简写属性，其设置了元素溢出时所需的行为——即当元素的内容太大而无法适应它的块级格,"/>
    
<link rel="stylesheet" href="/Bittersweet/css/diaspora.css">

    <script>window.searchDbPath = "/Bittersweet/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Bittersweet.的博客</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://gitee.com/yao-liang-8848"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">“什么是BFC”</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">“什么是BFC”</h1>
        <div class="stuff">
            <span>十一月 05, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/html/" rel="tag">html</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/web%E5%89%8D%E7%AB%AF/" rel="tag">web前端</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/%E6%95%99%E5%AF%BC%E6%96%87%E7%AB%A0/" rel="tag">教导文章</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/%E8%90%8C%E6%96%B0/" rel="tag">萌新</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="你以为我要先讲什么是bfc吗？"><a href="#你以为我要先讲什么是bfc吗？" class="headerlink" title="你以为我要先讲什么是bfc吗？"></a>你以为我要先讲什么是bfc吗？</h1><h2 id="1-overflow是什么？"><a href="#1-overflow是什么？" class="headerlink" title="1.overflow是什么？"></a>1.overflow是什么？</h2><blockquote>
<p>根据MDN的定义：**<code>overflow</code>** 是 <a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS">CSS</a> 的<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/Shorthand_properties">简写属性</a>，其设置了元素溢出时所需的行为——即当元素的内容太大而无法适应它的<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/Guide/CSS/Block_formatting_context">块级格式化上下文</a>时</p>
<p><strong>overflow</strong>默认属性是<strong>overflow: visible</strong>，即可见，不隐藏。换**overflow: hidden;**后溢出的元素就会被隐藏了</p>
<pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">&lt;!-- css -->
&lt;style>
.hiden</span><span class="token punctuation">&#123;</span>
 <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>
 <span class="token property">width</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
 <span class="token property">background-color</span><span class="token punctuation">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span>9<span class="token punctuation">,</span> 226<span class="token punctuation">,</span> 218<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token selector">.no</span><span class="token punctuation">&#123;</span>
 <span class="token property">width</span><span class="token punctuation">:</span> 10px<span class="token punctuation">;</span>
 <span class="token property">background-color</span><span class="token punctuation">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span>9<span class="token punctuation">,</span> 226<span class="token punctuation">,</span> 218<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
&lt;/style>
&lt;!-- html -->
&lt;body>
 &lt;div>
     &lt;p class=<span class="token string">"hiden"</span>>12345678910&lt;/p>
     &lt;p class=<span class="token string">"no"</span>>12345678910&lt;/p>
 &lt;/div>


&lt;/body><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

</blockquote>
<p>   <strong>效果如图</strong>：</p>
<blockquote>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/hiden.png"></p>
<p><strong>可以很明显的看到，第一个给了hiden的盒子，里面的超出了10px的东西都被隐藏，而第二个没有的，就突破了盒子的大小</strong></p>
</blockquote>
<h2 id="2-overflow是干嘛的？"><a href="#2-overflow是干嘛的？" class="headerlink" title="2.overflow是干嘛的？"></a>2.overflow是干嘛的？</h2><blockquote>
<h2 id=""><a href="#" class="headerlink" title=""></a></h2><h4 id="overflow用过的人都说好，那为什么好呢？"><a href="#overflow用过的人都说好，那为什么好呢？" class="headerlink" title="overflow用过的人都说好，那为什么好呢？"></a>overflow用过的人都说好，那为什么好呢？</h4><ol>
<li><p><strong>解决float造成的塌陷问题，overflow：hiden可以清楚浮动，原理就是触发bfc</strong></p>
<pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">&lt;!-- css样式 -->
&lt;style type="text/css">
    .box</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span>skyblue<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
    <span class="token selector">.kid</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span><span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">float</span><span class="token punctuation">:</span>left<span class="token punctuation">;</span><span class="token punctuation">&#125;</span>
    <span class="token selector">.kid1</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
    <span class="token selector">.kid2</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span> orange<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
    <span class="token selector">.wrap</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 150px<span class="token punctuation">;</span> <span class="token property">background</span><span class="token punctuation">:</span> blue<span class="token punctuation">;</span> <span class="token property">color</span><span class="token punctuation">:</span> white<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
&lt;/style>
    
&lt;!-- html -->
&lt;body>
    &lt;div class=<span class="token string">"box"</span>>
        &lt;div class=<span class="token string">"kid kid1"</span>>子元素1&lt;/div>
	&lt;div class=<span class="token string">"kid kid2"</span>>子元素2&lt;/div>
    &lt;/div>
    &lt;div class=<span class="token string">"wrap"</span>>其他部分&lt;/div>
&lt;/body><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="http://bittersweetyao.gitee.io/pic/pic/float%E5%A1%8C%E9%99%B7%E9%97%AE%E9%A2%98.png"></p>
</li>
</ol>
<p>可以看到，在没有overflow：hiden的情况下，其他部分被浮动的子元素1，2挡住了，而原因就是 由于父级元素没有高度，下面的元素会顶上去，造成页面的塌陷。因此，需要给父级加个overflow:hidden属性，这样父级的高度就随子级容器及子级内容的高度而自适应。</p>
<pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">&lt;!-- css样式 -->
&lt;style type="text/css">
    .box</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span>skyblue<span class="token punctuation">;</span> 
	  <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>  <span class="token comment">/*清除浮动*/</span>
	  <span class="token property">zoom</span><span class="token punctuation">:</span>1<span class="token punctuation">;</span> //兼容性设置，防止ie浏览器不兼容
        <span class="token punctuation">&#125;</span>
    <span class="token selector">.kid</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span><span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">float</span><span class="token punctuation">:</span>left<span class="token punctuation">;</span><span class="token punctuation">&#125;</span>
    <span class="token selector">.kid1</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
    <span class="token selector">.kid2</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span> orange<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
    <span class="token selector">.wrap</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 150px<span class="token punctuation">;</span> <span class="token property">background</span><span class="token punctuation">:</span> blue<span class="token punctuation">;</span> <span class="token property">color</span><span class="token punctuation">:</span> white<span class="token punctuation">;</span> <span class="token punctuation">&#125;</span>
&lt;/style>
 
&lt;body>
    &lt;div class=<span class="token string">"box"</span>>
        &lt;div class=<span class="token string">"kid kid1"</span>>子元素1&lt;/div>
	&lt;div class=<span class="token string">"kid kid2"</span>>子元素2&lt;/div>
    &lt;/div>
    &lt;div class=<span class="token string">"wrap"</span>>其他部分&lt;/div>
 &lt;body/>  <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


</blockquote>
<blockquote>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/overflow2.png"></p>
<p>那么可以看到，这次的其他部分就没有被子元素给挡住</p>
<p><strong>2. 解决外边距塌陷问题</strong></p>
<p>父级元素内部有子元素，如果给子元素添加margin-top样式，那么父级元素也会跟着下来，造成外边距塌陷，如下：</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>X-UA-Compatible<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>IE=edge<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>viewport<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>width=device-width, initial-scale=1.0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Document<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token comment">&lt;!-- css样式 --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>text/css<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css">
    <span class="token selector">.box</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span>skyblue<span class="token punctuation">;</span><span class="token punctuation">&#125;</span>
    <span class="token selector">.kid</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span><span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">background</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span> <span class="token property">margin-top</span><span class="token punctuation">:</span> 20px<span class="token punctuation">&#125;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span>
 
<span class="token comment">&lt;!-- html --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>box<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>kid<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>子元素1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


</blockquote>
<blockquote>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/margin%E5%A1%8C%E9%99%B7.png"></p>
<p>这里的子元素应该是与父元素在高度上有距离的，又边距的重叠导致效果无法出现的bug</p>
<p>使用overflow：hiden后如下：</p>
<pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">&lt;!-- css样式 -->
&lt;style type="text/css">
    .box</span><span class="token punctuation">&#123;</span> <span class="token property">background</span><span class="token punctuation">:</span>skyblue<span class="token punctuation">;</span>
          <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span> <span class="token comment">/*解决外边距塌陷*/</span>   
        <span class="token punctuation">&#125;</span>
    <span class="token selector">.kid</span><span class="token punctuation">&#123;</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span><span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">background</span><span class="token punctuation">:</span> yellow<span class="token punctuation">;</span> <span class="token property">margin-top</span><span class="token punctuation">:</span> 20px<span class="token punctuation">&#125;</span>
&lt;/style>
 
&lt;!-- html -->
&lt;body>
    &lt;div class=<span class="token string">"box"</span>>
	&lt;div class=<span class="token string">"kid"</span>>子元素1&lt;/div>
    &lt;/div>
&lt;/body><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


</blockquote>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/margin%E5%A1%8C%E9%99%B72.png"></p>
<p>这里就可以看到，问题解决了</p>
<h1 id="所以什么是BFC"><a href="#所以什么是BFC" class="headerlink" title="所以什么是BFC?"></a>所以什么是BFC?</h1><p><strong>上面我们使用的overflow：hiden解决的问题，实际上是触发了bfc机制，然后实现了我们想要的效果</strong></p>
<h2 id="1-bfc定义"><a href="#1-bfc定义" class="headerlink" title="1.bfc定义"></a>1.bfc定义</h2><p>套用官方的话：</p>
<p>BFC(Block formatting context)直译为”块级格式化上下文”。它是一个独立的渲染区域，只有Block-level box参与， 它规定了内部的Block-level Box如何布局，并且与这个区域外部毫不相干。</p>
<p>在解释什么是BFC之前，我们需要先知道Box、Formatting Context的概念。</p>
<h4 id="1-Box：css布局的基本单位"><a href="#1-Box：css布局的基本单位" class="headerlink" title="1.Box：css布局的基本单位"></a>1.Box：css布局的基本单位</h4><p>Box 是 CSS 布局的对象和基本单位， 直观点来说，就是一个页面是由很多个 Box 组成的。元素的类型和 display 属性，决定了这个 Box 的类型。 不同类型的 Box， 会参与不同的 Formatting Context（一个决定如何渲染文档的容器），因此Box内的元素会以不同的方式渲染。</p>
<h4 id="2-Formatting-Context"><a href="#2-Formatting-Context" class="headerlink" title="2.Formatting Context"></a>2.Formatting Context</h4><p>​	Formatting context 是 W3C CSS2.1 规范中的一个概念。它是页面中的一块渲染区域，并且有一套渲染规则，它决定了其子元素将如何定位，以及和其他元素的关系和相互作用。最常见的 Formatting context 有 Block fomatting context (简称BFC)和 Inline formatting context (简称IFC)。</p>
<blockquote>
<p><em><strong>BFC是一个独立的布局环境，其中的元素布局是不受外界的影响，并且在一个BFC中，块盒与行盒（行盒由一行中所有的内联元素所组成）都会垂直的沿着其父元素的边框排列。</strong></em></p>
</blockquote>
<h2 id="2-bfc布局规则"><a href="#2-bfc布局规则" class="headerlink" title="2.bfc布局规则"></a>2.bfc布局规则</h2><ol>
<li><p>内部的Box会在垂直方向，一个接一个地放置。</p>
</li>
<li><p>Box垂直方向的距离由<strong>margin决定</strong>。属于<strong>同一个BFC的两个相邻Box的margin会发生重叠。</strong></p>
</li>
<li><p>每个盒子（块盒与行盒）的margin box的左边，与包含块border box的左边相接触(对于从左往右的格式化，否则相反)。即使存在浮动也是如此。</p>
</li>
<li><p><strong>BFC的区域不会与float box重叠</strong>。</p>
</li>
<li><p>BFC就是页面上的一个<strong>隔离的独立容器</strong>，容器里面的子元素不会影响到外面的元素。反之也如此。</p>
</li>
<li><p>计算BFC的高度时，浮动元素也参与计算。</p>
</li>
</ol>
<h2 id="3-如何创建bfc"><a href="#3-如何创建bfc" class="headerlink" title="3.如何创建bfc"></a>3.如何创建bfc</h2><ul>
<li>1、float的值不是none。</li>
<li>2、position的值不是static或者relative。</li>
<li>3、display的值是inline-block、table-cell、flex、table-caption或者inline-flex</li>
<li>4、<strong>overflow的值不是visible</strong></li>
</ul>
<h2 id="4-bfc的作用"><a href="#4-bfc的作用" class="headerlink" title="4.bfc的作用"></a>4.bfc的作用</h2><h4 id="1-解决float造成的塌陷问题与margin边框重叠，方案就在上面的overflow上"><a href="#1-解决float造成的塌陷问题与margin边框重叠，方案就在上面的overflow上" class="headerlink" title="1.解决float造成的塌陷问题与margin边框重叠，方案就在上面的overflow上"></a>1.解决float造成的塌陷问题与margin边框重叠，方案就在上面的overflow上</h4><h4 id="2-实现自适应两栏："><a href="#2-实现自适应两栏：" class="headerlink" title="2.实现自适应两栏："></a>2.实现自适应两栏：</h4><p><strong>因为：每个盒子的margin box的左边，与包含块border box的左边相接触(对于从左往右的格式化，否则相反)。即使存在浮动也是如此</strong> </p>
<p>又因为：BFC的区域不会与float box重叠，所以我可以让right单独触发一个bfc事件</p>
<pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UTF-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>viewport<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>width=device-width, initial-scale=1.0<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>X-UA-Compatible<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ie=edge<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Document<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css">
    <span class="token selector">*</span><span class="token punctuation">&#123;</span>
        <span class="token property">margin</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
        <span class="token property">padding</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
    <span class="token selector">body</span> <span class="token punctuation">&#123;</span>
        <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span>
        <span class="token property">position</span><span class="token punctuation">:</span> relative<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
 
    <span class="token selector">.left</span> <span class="token punctuation">&#123;</span>
        <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span> 150px<span class="token punctuation">;</span>
        <span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span>
        <span class="token property">background</span><span class="token punctuation">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span>139<span class="token punctuation">,</span> 214<span class="token punctuation">,</span> 78<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token property">line-height</span><span class="token punctuation">:</span> 150px<span class="token punctuation">;</span>
        <span class="token property">font-size</span><span class="token punctuation">:</span> 20px<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
 
    <span class="token selector">.right</span> <span class="token punctuation">&#123;</span>
        <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span>
        <span class="token property">height</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span>
        <span class="token property">background</span><span class="token punctuation">:</span> <span class="token function">rgb</span><span class="token punctuation">(</span>170<span class="token punctuation">,</span> 54<span class="token punctuation">,</span> 236<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token property">text-align</span><span class="token punctuation">:</span> center<span class="token punctuation">;</span>
        <span class="token property">line-height</span><span class="token punctuation">:</span> 300px<span class="token punctuation">;</span>
        <span class="token property">font-size</span><span class="token punctuation">:</span> 40px<span class="token punctuation">;</span>
    <span class="token punctuation">&#125;</span>
</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>style</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>left<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>LEFT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>right<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>RIGHT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="http://bittersweetyao.gitee.io/pic/pic/%E8%87%AA%E9%80%82%E5%BA%94%E4%B8%A4%E6%A0%8F2.png"></p>
<p><strong>right会自动的适应宽度，这时候就形成了一个两栏自适应的布局</strong></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p><strong>以上例子都体现了：</strong></p>
<blockquote>
<p><strong>BFC就是页面上的一个隔离的独立容器，容器里面的子元素不会影响到外面的元素。反之也如此。</strong></p>
</blockquote>
<p>因为BFC内部的元素和外部的元素绝对不会互相影响，因此， 当BFC外部存在浮动时，它不应该影响BFC内部Box的布局，BFC会通过变窄，而不与浮动有重叠。同样的，当BFC内部有浮动时，为了不影响外部元素的布局，BFC计算高度时会包括浮动的高度。避免margin重叠也是这样的一个道理。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
        <div class="side">
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%A0%E4%BB%A5%E4%B8%BA%E6%88%91%E8%A6%81%E5%85%88%E8%AE%B2%E4%BB%80%E4%B9%88%E6%98%AFbfc%E5%90%97%EF%BC%9F"><span class="toc-number">1.</span> <span class="toc-text">你以为我要先讲什么是bfc吗？</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-overflow%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">1.1.</span> <span class="toc-text">1.overflow是什么？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-overflow%E6%98%AF%E5%B9%B2%E5%98%9B%E7%9A%84%EF%BC%9F"><span class="toc-number">1.2.</span> <span class="toc-text">2.overflow是干嘛的？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link"><span class="toc-number">1.3.</span> <span class="toc-text"></span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#overflow%E7%94%A8%E8%BF%87%E7%9A%84%E4%BA%BA%E9%83%BD%E8%AF%B4%E5%A5%BD%EF%BC%8C%E9%82%A3%E4%B8%BA%E4%BB%80%E4%B9%88%E5%A5%BD%E5%91%A2%EF%BC%9F"><span class="toc-number">1.3.0.1.</span> <span class="toc-text">overflow用过的人都说好，那为什么好呢？</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%89%80%E4%BB%A5%E4%BB%80%E4%B9%88%E6%98%AFBFC"><span class="toc-number">2.</span> <span class="toc-text">所以什么是BFC?</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-bfc%E5%AE%9A%E4%B9%89"><span class="toc-number">2.1.</span> <span class="toc-text">1.bfc定义</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-Box%EF%BC%9Acss%E5%B8%83%E5%B1%80%E7%9A%84%E5%9F%BA%E6%9C%AC%E5%8D%95%E4%BD%8D"><span class="toc-number">2.1.0.1.</span> <span class="toc-text">1.Box：css布局的基本单位</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-Formatting-Context"><span class="toc-number">2.1.0.2.</span> <span class="toc-text">2.Formatting Context</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-bfc%E5%B8%83%E5%B1%80%E8%A7%84%E5%88%99"><span class="toc-number">2.2.</span> <span class="toc-text">2.bfc布局规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BAbfc"><span class="toc-number">2.3.</span> <span class="toc-text">3.如何创建bfc</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-bfc%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="toc-number">2.4.</span> <span class="toc-text">4.bfc的作用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E8%A7%A3%E5%86%B3float%E9%80%A0%E6%88%90%E7%9A%84%E5%A1%8C%E9%99%B7%E9%97%AE%E9%A2%98%E4%B8%8Emargin%E8%BE%B9%E6%A1%86%E9%87%8D%E5%8F%A0%EF%BC%8C%E6%96%B9%E6%A1%88%E5%B0%B1%E5%9C%A8%E4%B8%8A%E9%9D%A2%E7%9A%84overflow%E4%B8%8A"><span class="toc-number">2.4.0.1.</span> <span class="toc-text">1.解决float造成的塌陷问题与margin边框重叠，方案就在上面的overflow上</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%AE%9E%E7%8E%B0%E8%87%AA%E9%80%82%E5%BA%94%E4%B8%A4%E6%A0%8F%EF%BC%9A"><span class="toc-number">2.4.0.2.</span> <span class="toc-text">2.实现自适应两栏：</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">总结</span></a></li></ol>
        </div>
    
</div>


    </div>
</div>
<script src="/Bittersweet/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/Bittersweet/live2dw/assets/assets/koharu.model.json"},"display":{"position":"right","width":200,"height":400},"mobile":{"show":true},"react":{"opacity":0.8},"log":false});</script></body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/Bittersweet/js/plugin.js"></script>
<script src="/Bittersweet/js/typed.js"></script>
<script src="/Bittersweet/js/diaspora.js"></script>


<link rel="stylesheet" href="/Bittersweet/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/Bittersweet/photoswipe/default-skin/default-skin.css">


<script src="/Bittersweet/photoswipe/photoswipe.min.js"></script>
<script src="/Bittersweet/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>




</html>
